<?php

//Startar sessionen och inkluderar filer
session_start();

require_once ('Controller/LoginController.php');
require_once ('Model/Database.php');
require_once('Model/DBConfig.php');
require_once ('Model/FriendHandler.php');
require_once('View/PageView.php');
require_once ('Model/User.php');
require_once ('Model/UserHandler.php');
require_once ('Model/Movie.php');
require_once ('Model/MovieHandler.php');
require_once ('Controller/RegisterController.php');
require_once ('Controller/MovieListController.php');
require_once ('Controller/AddMovieController.php');
require_once ('Controller/MovieController.php');
require_once ('View/PageCompositionView.php');
require_once ('View/RegisterView.php');
require_once ('View/NavigationView.php');
require_once ('View/UserView.php');
require_once ('View/MovieFeedView.php');
require_once ('Controller/UserController.php');
require_once('View/LoginView.php');
require_once('View/MovieView.php');
require_once ('Model/LoginHandler.php');
require_once ('Controller/MovieFeedController.php');
require_once ('Model/MovieFeedHandler.php');
require_once ('Controller/FriendController.php');
require_once ('View/FriendView.php');
require_once ('View/MenuView.php');


class MasterController {
	
	public static function doControll(){
			
		//Skapar och initierar databasen
		$db = new \Model\Database();
		$db->Connect(new \Model\DBConfig());
		
		$navView = new \View\NavigationView();
		$pcv = new \View\PageCompositionView();
		$userHandler = new\Model\UserHandler($db);
		$movieHandler = new\Model\MovieHandler($db);
		$movieFeedHandler = new\Model\MovieFeedHandler($db);
		
		//Tar med hjälp av navigationView reda på vilken controller som ska anropas
		if ($navView->isViewingUserPage()==false) {
			
			$registerView = new \View\RegisterView();
			$registerController = new \Controller\RegisterController();
			$loginHtml = $registerController->DoControl($db);
				
			$loginController = new \Controller\LoginController();
			$registerHtml = $loginController->DoControll($db);
			
			$html=$pcv->merge($loginHtml, $registerHtml);
			
		}
		
		if ($navView->isViewingUserPage()==true) {
			
			$userController = new \Controller\UserController();
			
			$userHtml = $userController->doControll($db, $userHandler);
			$movieListController = new\Controller\MovieListController();
			$movieHtml = $movieListController->doControll($db, $userHandler, $movieHandler); 
			
			
			$html= $pcv->merge($userHtml, $movieHtml );
			
		} 
		
		if ($navView->IsViewingMovie()){
			
			$movieController = new\Controller\MovieController();
			$html= $movieController->doControll($db, $movieHandler);
		}
		
		if ($navView->IsMovieControllerActive()){
			
			$addMovieController = new\Controller\AddMovieController();
			$html= $addMovieController->doControll($db, $userHandler); 
			
		}
		
		if ($navView->IsMovieFeedControllerActive()){
			$movieFeedController = new\Controller\MovieFeedController();
			$html= $movieFeedController->doControll($db, $movieFeedHandler, $userHandler); 
		}
		
		if ($navView->IsFriendControllerActive()){
			$friendController = new\Controller\FriendController();
			$friendHandler = new\Model\FriendHandler($db);
			$html= $friendController->doControll($db, $friendHandler, $userHandler); 
		}
		
		
		$pageView = new \View\PageView();
		
		$title = "Moviebook";
		
		//Stänger databasen
		$db->Close();
		
		//lägger till stylesheet
		$pageView->AddStyleSheet("Basic.css");
		//anropar pageview för HTML-sida
		return $pageView->GetHTMLPage($title, $html); 
	}
}
echo MasterController::doControll();

